﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SmartBlast.GUI;

namespace SmartBlast
{
    public partial class frmLogin : Form
    {
        private static string TABLE_NAME = "Employees";
        private DataBase_BUS databaseBUS;
        private static string[] FIELD_NAMES = new string[] { "Username", "Password"};

        public frmLogin(frmConnectServer connectServer)
        {
            
            InitializeComponent();
            databaseBUS = DataBase_BUS.getInstance();
            
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (txtUser.Text == "" || txtPass.Text == "")
            {
                MessageBox.Show("\nMissing Fields!", ":(", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            string[] fields = { "'" + txtUser.Text + "'", "'" + txtPass.Text + "'" };

            DataTable loginDataTable = databaseBUS.selectRecrordFromTable(TABLE_NAME, FIELD_NAMES, fields);

            if (loginDataTable.Rows.Count == 0)
            {
                MessageBox.Show("\nInvalid Login", ":(", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                goToMainWindow();
            }
        }

        private void goToMainWindow()
        {
            Main main = new Main();
            main.Show();
            this.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            goToMainWindow();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
        }

        private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }



    }
}
